package com.creditnet.bmc.platform.controller;

import com.creditnet.bmc.common.utils.Result;
import com.creditnet.bmc.platform.domain.UserOnline;
import com.creditnet.bmc.platform.service.SessionService;
import org.apache.shiro.session.Session;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.mvc.support.RedirectAttributes;

import java.util.Collection;
import java.util.List;

/**
 * Created by bjmaozhengwei on 2018/4/1.
 */
@RequestMapping("/sys/online")
@Controller
public class SessionController {

    @Autowired
    SessionService sessionService;

    /**
     * SHOW ONLINE PAGE
     * @return
     */
    @GetMapping()
    public String online() {
        return "system/online/online";
    }

    /**
     * Show Online User
     * @return
     */
    @ResponseBody
    @RequestMapping("/list")
    public List<UserOnline> list() {
        return sessionService.list();
    }

    @ResponseBody
    @RequestMapping("/forceLogout/{sessionId}")
    public Result forceLogout(@PathVariable("sessionId") String sessionId, RedirectAttributes redirectAttributes) {
        try {
            sessionService.forceLogout(sessionId);
            return Result.ok();
        } catch (Exception e) {
            e.printStackTrace();
            return Result.error();
        }
    }

    @ResponseBody
    @RequestMapping("/sessionList")
    public Collection<Session> sessionList() {
        return sessionService.sessionList();
    }

}
